package mobileann.mafamily.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import mobileann.mafamily.act.main.FS;
import mobileann.mafamily.db.model.EZoneDBModel;
import mobileann.mafamily.db.model.WeekPlanDBModel;
import mobileann.mafamily.entity.EleEntity;
import mobileann.mafamily.entity.MemberInfoEntity;
import mobileann.mafamily.entity.UserInfoEntity;
import mobileann.mafamily.entity.WeekPlanBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.mobileann.mafamily.R;
import com.mofind.android.base.L;
import com.mofind.android.net.HttpUtils;
import com.mofind.android.net.HttpUtils.HttpResultCallback;
import com.mofind.android.net.JSONParser;

/**
 * 
 * @change ADRIAN
 */
public class LoginUtils {
	private static LoginUtils _instance;
	private String TAG = "LoginUtils";
	public static final int GET_CAPTCHA_SUCCESS = 640;
	public static final int GET_CAPTCHA_ERROR = 641;
	public static final int LOGIN_SUCCESS = 642;
	public static final int LOGIN_ERROR = 643;
	public static final int WEEKPLAN_NULL = 644;
	public static final int WEEKPLAN_SUCCESS = 645;
	public static final int ELE_NULL = 646;
	public static final int ELE_SUCCESS = 647;
	public static final int DEVICE_ERROR = 648;
	public static final int DEVICE_SUCCESS = 649;

	public synchronized static LoginUtils getInstance() {
		if (_instance == null) {
			_instance = new LoginUtils();
		}
		return _instance;
	}

	/**
	 * 获取验证码时上传的参数
	 * 
	 * @param ut
	 *            时间戳
	 * @param tel
	 *            手机号码
	 * @param type
	 *            注册登录/取回密码/修改手机号/短信添加成员
	 * @return
	 */
	private static Map<String, String> getCaptchaPara(String ut, String tel,
			String type) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "get_code");
		map.put("ut", ut);
		map.put("data",
				TripleDESUtil.CBCEncode(ut, "get_code",
						getCaptchaJsonData(tel, type)));
		return map;
	}

	private static String getCaptchaJsonData(String tel, String type) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("phone",  tel );
		map.put("type",  type );
		String data = MyTaskUtils.getInstance().map2json(map);
		return data;
	}

	/**
	 * 向服务器请求获取验证码
	 * 
	 * @param ut
	 * @param tel
	 * @param type
	 */
	public void doGetCaptcha(final String ut, String tel, final String type,
			final Handler handler) {
		HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD,
				getCaptchaPara(ut, tel, type), new HttpUtils.HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				String json = TripleDESUtil.getJsonObjStr(ut, "get_code", jsonStr);
				if(!TextUtils.isEmpty(json)){
					JSONObject jo = JSONParser.getJSONObject(json);
					String status = JSONParser.getString(jo, "status");
					String errorStr = null;
					if ("false".equals(status)) {
						String reason = JSONParser.getString(jo, "reason");
						if ("format".equals(reason)) {
							errorStr = "手机号码输入有误，请重新输入";
						} else if ("none".equals(reason)) {
							//        				if (type.equals("password")) {
							//        				    errorStr = "取回密码不存在该号码";
							//        				} else if (type.equals("phone")) {
							//        				    errorStr = "修改手机号码不存在该号码";
							//        				}
							errorStr="找回密码的手机号码输入有误，请重新输入";
						} else if ("send_limit".equals(reason)) {
							errorStr = "验证码获取过于频繁，已达本日上限";
						} else if ("time_limit".equals(reason)) {
							//				errorStr = "同一号码多次获取验证码间隔小于设定的值";
							errorStr = "获取短信验证码频繁，请稍后再试";
						} else if ("other".equals(reason)) {
							errorStr = "服务器开小差了，请稍后重试…";
						}
						if(handler!=null)
							handler.obtainMessage(GET_CAPTCHA_ERROR, errorStr).sendToTarget();
					} else if ("true".equals(status)) {
						if(handler!=null)
							handler.obtainMessage(GET_CAPTCHA_SUCCESS).sendToTarget();
					}
				}else{//获取失败
					if(handler!=null){
						String error="请稍后重试";
						handler.obtainMessage(GET_CAPTCHA_ERROR, error).sendToTarget();
					}
				}
			}
		});
	}


	public void getLoginInfo(final String tel, final String captchas,
			final Handler handler) {
		String getLoginInfo = URLUtils.URL_TASK_GET + tel + "_" + captchas + "_login";

		// TODO ADRIAN 这里有可能是获取详细信息
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET + tel + "_" + captchas
				+ "_login", new HttpUtils.HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				String json = TripleDESUtil.getJsonObjStr(tel, captchas,
						jsonStr);
				SPUtils.setVerifyCode(captchas);
				JSONObject jo = JSONParser.getJSONObject(json);
				String status = JSONParser.getString(jo, "status");
				if (!TextUtils.isEmpty(status)) {
					handler.obtainMessage(LOGIN_ERROR).sendToTarget();
				} else {
					String uid = JSONParser.getString(jo, "uid");
					String name = JSONParser.getString(jo, "name");
					String urad = JSONParser.getString(jo, "urad");
					String fid = JSONParser.getString(jo, "fid");
					String frad = JSONParser.getString(jo, "frad");
					String role = JSONParser.getString(jo, "role");
					String avatar = JSONParser.getString(jo, "avatar");
					String irad = JSONParser.getString(jo, "irad");
					String lockpwd = JSONParser.getString(jo, "lockpwd");
					//new 推荐码
					String referee_code = JSONParser.getString(jo, "referee_code");
					SPUtils.setRefereeCode(uid, referee_code);
					MemberInfoEntity entity = new MemberInfoEntity(uid, urad,
							name, role, tel, avatar);
					entity.setFrad(frad);
					entity.setFid(fid);
					entity.setIrad(irad);
					if (lockpwd != null) {
						entity.setLockpwd(lockpwd);
					}
					SPUtils.saveMySelf(entity);
					// 为使用老版本加入
					UserInfoEntity userInfo = new UserInfoEntity();
					userInfo.setUid(tel);
					userInfo.setFid(fid);
					userInfo.setNickname(name);
					userInfo.setRole(Integer.parseInt(role));
					SPUtils.saveSelf(userInfo);
					FS.getInstance().self().setTelephone(tel);
					MemberListUtils.getInstance().setCurrentUser(userInfo);
					if(handler!=null)
						handler.obtainMessage(LOGIN_SUCCESS).sendToTarget();
				}
			}
		});
	}

	/**判断此号码是否有效 @ADRIAN20151021*/
	public boolean validate(String tel, TextView view) {
		boolean isShowTips = false;
		if(PhoneNumUtil.isMobileNo(tel)){
			view.setVisibility(View.GONE);
			isShowTips=false;
			return true;
		}
		if (NetUtils.getInstance().netstate() == 0) {
			view.setVisibility(View.VISIBLE);
			view.setText("当前没有网络连接,请检查后重试");
			isShowTips = true;
			return false;
		}
		if (TextUtils.isEmpty(tel)) {
			view.setVisibility(View.VISIBLE);
			view.setText("帐号不能为空!");
			isShowTips = true;
			return false;
		}
		if (tel.length() != 11) {
			view.setVisibility(View.VISIBLE);
			view.setText("帐号长度为11位!");
			isShowTips = true;
			return false;
		}
		if (isShowTips) {
			view.setVisibility(View.VISIBLE);
		} else {
			view.setVisibility(view.GONE);
		}
		return true;
	}

	private int second = 0;
	private Timer timer;
	private TimerTask task;

	public void getCodeOnClick(final Activity act, final TextView view) {
		timer = new Timer();
		// 启动计时器
		second = 30;
		task = new TimerTask() {
			@Override
			public void run() {
				second--;
				act.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						view.setTextColor(act.getResources().getColor(
								R.color.black5));
						view.setText(second + "秒后重新获取");
						view.setEnabled(false);
						if (second <= 0) {
							view.setTextColor(act.getResources().getColor(
									R.color.login_bg));
							view.setText("获取验证码");
							view.setEnabled(true);
							if (timer != null) {
								timer.cancel();
								timer = null;
								task = null;
							}
						}
					}
				});
			}
		};
		timer.schedule(task, 20, 1000);
	}

	private Thread thdTimeout = null;

	public void setTimeOut(final Handler handler) {
		cancelTimeOut();
		thdTimeout = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(10000);
					handler.obtainMessage(123).sendToTarget();
				} catch (InterruptedException e) {
					L.e("maf", "error--", e);
				}
			}
		});
		thdTimeout.start();
	}

	public void cancelTimeOut() {
		if (thdTimeout != null) {
			thdTimeout.interrupt();
			try {
				thdTimeout.join(3600);
			} catch (InterruptedException e) {
				L.e("maf", "error--", e);
			}
			thdTimeout = null;
		}
	}

	public void progressShow(View progressView, TextView tv_pro) {
		progressView.setVisibility(View.VISIBLE);
		tv_pro.setText("正在登录...");
	}

	public void progressDismiss(View progressView) {
		progressView.setVisibility(View.GONE);
	}

	/**
	 * 获取策略
	 * 
	 * @param uid
	 *            想要获取的成员的uid
	 * @param urad
	 *            想要获取的成员的urad
	 * @param handler
	 */
	public void getLockPolicy(final Context c,final String uid, final String urad, final Handler handler) {
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET + uid + "_" + urad
				+ "_policy", new HttpUtils.HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				List<WeekPlanBean> weekPlans = new ArrayList<WeekPlanBean>();
				try {
					String json = TripleDESUtil.getJsonArrStr(uid, urad,
							jsonStr);
					if(!TextUtils.isEmpty(json)){
						JSONArray ja = JSONParser.getJSONArray(json);
						if (ja.length() == 0) {
							WeekPlanDBModel.clear(c);
							if(handler != null){
								handler.obtainMessage(WEEKPLAN_NULL).sendToTarget();
							}
						} else {
							WeekPlanDBModel.clear(c);
							for (int i = 0; i < ja.length(); i++) {
								WeekPlanBean weekPlan = new WeekPlanBean();
								JSONObject jo = ja.getJSONObject(i);
								String plan_id = JSONParser.getString(jo, "id");
								String start_time = JSONParser.getString(jo, "start_time");
								String stop_time = JSONParser.getString(jo, "stop_time");
								String week = JSONParser.getString(jo, "week");
								String enabled = JSONParser.getString(jo, "enable");
								weekPlan.setPlan_id(plan_id);
								weekPlan.setStart_time(start_time);
								weekPlan.setEnd_time(stop_time);
								weekPlan.setWeek(week);
								weekPlan.setEnabled(enabled);
								weekPlan.setFamily_id(SPUtils.getMySelf(SPUtils.MYFID));
								//			    weekPlan.setFamily_id(SPUtils.getFID2());
								//			    weekPlan.setUser_id(SPUtils.getUID());
								weekPlan.setUser_id(SPUtils.getMySelf(SPUtils.MYPHONE));
								//new
								weekPlan.setTarget_id(uid);
								weekPlans.add(weekPlan);
								WeekPlanDBModel.insertPlan(c, weekPlan);
							}
							if (handler != null) {
								handler.obtainMessage(WEEKPLAN_SUCCESS, weekPlans).sendToTarget();
							}
						}
					}else{
						L.e(TAG, "获取锁屏计划失败");
					}
				} catch (JSONException e) {
					L.e("maf", "error--", e);
				}
			}
		});
	}

	public void getEle(final Context c,final String uid, final String urad,
			final Handler handler) {
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET + uid + "_" + urad
				+ "_fence", new HttpUtils.HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				String json = TripleDESUtil.getJsonArrStr(uid, urad, jsonStr);
				if(!TextUtils.isEmpty(json)){
					try {
						JSONArray ja = JSONParser.getJSONArray(json);
						List<EleEntity> eles = new ArrayList<EleEntity>();
						if (ja.length() == 0) {
							if(handler!=null)
								handler.obtainMessage(ELE_NULL).sendToTarget();
						} else {
							EZoneDBModel.deleteEZoneTable(c);
							for (int i = 0; i < ja.length(); i++) {
								JSONObject jo = ja.getJSONObject(i);
								String id = JSONParser.getString(jo, "id");
								String lon = JSONParser.getString(jo, "longitude");
								String lat = JSONParser.getString(jo, "latitude");
								String radius = JSONParser.getString(jo, "radius");
								String desc = JSONParser.getString(jo, "desc");
								String enabled = JSONParser.getString(jo, "enable");
								EleEntity ele = new EleEntity(i+"", id, SPUtils.getUID(),
										SPUtils.getMySelf(SPUtils.MYFID), lon, lat, radius, enabled, desc);
								eles.add(ele);
								EZoneDBModel.insertEZone(c, ele);
							}
							if(handler!=null)
								handler.obtainMessage(ELE_SUCCESS, eles).sendToTarget();
						}
					} catch (JSONException e) {
						L.e("maf", "error--", e);
					}
				}
			}
		});
	}

	/*
	 * 截取短信中的验证码
	 */
	public String getVerifyCode(String smsContent) {
		Pattern numberPattern = Pattern.compile("[0-9\\.]+");
		Matcher m = numberPattern.matcher(smsContent);
		String verifyCode = "";
		List<String> codes=new ArrayList<String>();
		while (m.find()) {
			codes.add(m.group());
		}
		if(codes.size()!=0){
			verifyCode=codes.get(0);
		}
		return verifyCode;
	}

	private Map<String, String> getLoginPara(Context c, String tel, String code, String ut) {
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("channel", PhoneInfoUtils.getVersionChannel(c));
		map1.put("version", PhoneInfoUtils.getVersionCode(c));
		map1.put("type", "phone");
		map1.put("phone", tel);
		map1.put("code", code);
		String data = MyTaskUtils.getInstance().map2json(map1);
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "register");
		map.put("ut", ut);
		map.put("data", TripleDESUtil.CBCEncode(ut, "register", data));
		return map;
	}

	/**
	 * 2015.7.15.18.02.登录擦操作，具体不详
	 * 2015.7.15.20.02.每次登录先从MA的服务器去获取数据
	 * 2015.7.15.20.14.好像未调用此方法
	 * @param c
	 * @param tel
	 * @param code
	 * @param ut
	 * @param handler
	 * @annotation ADRIAN 2015.7.15.18.35
	 */
	public void doLogin(Context c, final String tel, final String code, final String ut,final Handler handler) {
		String doLogin = URLUtils.URL_GROWTH_UPLOAD;

		HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD,
				getLoginPara(c, tel, code, ut), new HttpUtils.HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult,
					String jsonStr) {
				//			String json = TripleDESUtil.getJsonObjStr(ut, "register",
				//				jsonStr);
				SPUtils.setVerifyCode(code);
				////			L.i("login","LLL"+json);
				//			if(TextUtils.isEmpty(json)){
				//			    L.e(TAG, "LOGIN ERROR");
				//			}else{
				//			JSONObject jo = JSONParser.getJSONObject(json);
				//			String status = JSONParser.getString(jo, "status");
				//			if (!TextUtils.isEmpty(status)) {
				//			    String reason=JSONParser.getString(jo, "reason");
				//			    String reaStr=null;
				//			    if("code".equals(reason)){
				//				reaStr="验证码错误";
				//			    }else if("other".equals(reason)){
				//				reaStr="其他原因";
				//			    }else if("disabled".equals(reason)){
				//				reaStr="验证码已失效";
				//			    }else if("expired".equals(reason)){
				//				reaStr="验证码已过期";
				//			    }else {
				//				reaStr="ERROR";
				//			    }
				//			    if(handler!=null)
				//				handler.obtainMessage(LOGIN_ERROR,reaStr).sendToTarget();
				//			} else {
				//			    String uid = JSONParser.getString(jo, "uid");
				//			    String name = JSONParser.getString(jo, "name");
				//			    String urad = JSONParser.getString(jo, "urad");
				//			    String fid = JSONParser.getString(jo, "fid");
				//			    String frad = JSONParser.getString(jo, "frad");
				//			    String role = JSONParser.getString(jo, "role");
				//			    String avatar = JSONParser.getString(jo, "avatar");
				//			    String irad = JSONParser.getString(jo, "irad");
				//			    String lockpwd = JSONParser.getString(jo, "lockpwd");
				//			    String phone=JSONParser.getString(jo, "phone");
				//			    MemberInfoEntity entity = new MemberInfoEntity(uid, urad,
				//				    name, role, phone, avatar);
				//			    entity.setFrad(frad);
				//			    entity.setFid(fid);
				//			    entity.setIrad(irad);
				//			    if (lockpwd != null) {
				//				entity.setLockpwd(lockpwd);
				//			    }
				//			    SPUtils.saveMySelf(entity);
				//			    // 为使用老版本加入
				//			    UserInfoEntity userInfo = new UserInfoEntity();
				//			    userInfo.setUid(tel);
				//			    userInfo.setFid(fid);
				//			    userInfo.setNickname(name);
				//			    userInfo.setTelephone(phone);
				//			    userInfo.setRole(Integer.parseInt(role));
				//			    if (lockpwd != null) {
				//				userInfo.setScLockPwd(lockpwd);
				//			    }
				//			    SPUtils.saveSelf(userInfo);
				//			    FS.getInstance().self().setTelephone(tel);
				//			    FS.getInstance().self().setUid(tel);
				//			    FS.getInstance().self().setRole(Integer.parseInt(role));
				//			    FS.getInstance().self().setNickname(name);
				//			    FS.getInstance().self().setFid(fid);
				//			    MemberListUtils.getInstance().setCurrentUser(userInfo);
				//			    if(handler!=null)
				//				handler.obtainMessage(LOGIN_SUCCESS).sendToTarget();
				//			}
				//		    }
				setLoginData(ut, jsonStr, handler,"phone");
			}
		});
	}
	public void getDeviceInfo(final String fid,final String frad,final Handler handler){
		HttpUtils.startHttpGet(URLUtils.URL_TASK_GET+fid+"_"+frad+"_device", new HttpResultCallback() {

			@Override
			public void result(String url, boolean isResult, String jsonStr) {
				String json = TripleDESUtil.getJsonArrStr(fid, frad, jsonStr);
				if(TextUtils.isEmpty(json)){
					if(handler!=null)
						handler.obtainMessage(DEVICE_ERROR).sendToTarget();
				}else{
					JSONObject jo = JSONParser.getJSONObject(json);
					String status=JSONParser.getString(jo, "status");
					if(!TextUtils.isEmpty(status)){
						if(handler!=null)
							handler.obtainMessage(DEVICE_ERROR).sendToTarget();
					}else{
						JSONArray ja=JSONParser.getJSONArray(json);
						for(int i=0;i<ja.length();i++){
							try {
								JSONObject jon = ja.getJSONObject(i);
								String uid=JSONParser.getString(jon, "uid");
								String os=JSONParser.getString(jon, "os");
								SPUtils.setDeviceType(uid, os);
								if(handler!=null)
									handler.obtainMessage(DEVICE_SUCCESS).sendToTarget();
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
	}

	/**
	 * 2015.7.15。20.03.解析从服务器下载来的json字符串，将Handler传入，还有type类型
	 * <br>uid    用户的uid
	 * <br>name   昵称
	 * <br>urad   用户随机码
	 * <br>fid    家庭id
	 * <br>frad   家庭随机码
	 * <br>role   角色（取值为“-1”的时候表示新用户没有分配角色）
	 * <br>avatar 头像地址http或去，没有为空值
	 * <br>irad   用户积分随机码
	 * <br>lockpwd 解锁密码
	 * <br>phone   电话号码
	 * <br>referee_code new 推荐码
	 * @param ut
	 * @param jsonStr
	 * @param handler
	 * @param type
	 * @change ADRIAN 
	 */
	public void setLoginData(String ut,String jsonStr,Handler handler,String type){
		String json = TripleDESUtil.getJsonObjStr(ut, "register", jsonStr);

		if(TextUtils.isEmpty(json)){
			L.e(TAG, "LOGIN ERROR");
		}else{
			JSONObject jo = JSONParser.getJSONObject(json);
			String status = JSONParser.getString(jo, "status");
			if (!TextUtils.isEmpty(status)) {
				String reason=JSONParser.getString(jo, "reason");
				String reaStr=null;
				if("code".equals(reason)){
					reaStr="短信验证码输入有误，请重新输入";
				}else if("other".equals(reason)){
					reaStr="服务器开小差了，请稍后重试…";
				}else if("disabled".equals(reason)){
					reaStr="短信验证码已过期，请重新获取";
				}else if("expired".equals(reason)){
					reaStr="短信验证码已过期，请重新获取";
				}else {
					reaStr="ERROR";
				}
				if(handler!=null)
					handler.obtainMessage(LOGIN_ERROR,reaStr).sendToTarget();
			} else {
				String uid = JSONParser.getString(jo, "uid");
				String name = JSONParser.getString(jo, "name");
				String urad = JSONParser.getString(jo, "urad");
				String fid = JSONParser.getString(jo, "fid");
				String frad = JSONParser.getString(jo, "frad");
				String role = JSONParser.getString(jo, "role");
				String avatar = JSONParser.getString(jo, "avatar"); 
				String irad = JSONParser.getString(jo, "irad");
				String lockpwd = JSONParser.getString(jo, "lockpwd");
				String phone=JSONParser.getString(jo, "phone");
				//new 推荐码
				String referee_code = JSONParser.getString(jo, "referee_code");
				SPUtils.setRefereeCode(uid, referee_code);
				MemberInfoEntity entity = new MemberInfoEntity(uid, urad, name, role, phone, avatar);
				entity.setFrad(frad);
				entity.setFid(fid);
				entity.setIrad(irad);
				if (lockpwd != null) {
					entity.setLockpwd(lockpwd);
				}
				SPUtils.saveMySelf(entity);
				// 为使用老版本加入
				UserInfoEntity userInfo = new UserInfoEntity();
				userInfo.setUid(phone);
				userInfo.setFid(fid);
				userInfo.setNickname(name);
				userInfo.setTelephone(phone);
				userInfo.setRole(Integer.valueOf(role));
				if (lockpwd != null) {
					userInfo.setScLockPwd(lockpwd);
				}
				SPUtils.saveSelf(userInfo);
				FS.getInstance().self().setTelephone(phone);
				FS.getInstance().self().setUid(phone);
				FS.getInstance().self().setRole(Integer.parseInt(role));
				FS.getInstance().self().setNickname(name);
				FS.getInstance().self().setFid(fid);
				MemberListUtils.getInstance().setCurrentUser(userInfo);
				SPUtils.setLoginType(uid, type);
				if(handler!=null)
					handler.obtainMessage(LOGIN_SUCCESS).sendToTarget();
				//	    SPUtils.setLockStart(true);
			}
		}
	}
	public void versionUpdate(Activity context,Handler handler){
		//	FS.getInstance().cleanData(FS.getInstance());
		String loginType=SPUtils.getLoginType(SPUtils.getMySelf(SPUtils.MYUID));
		String openid=SPUtils.getRdLoginOpenId();
		String access=SPUtils.getRdLoginAccess();
		String refresh=SPUtils.getRdLoginRefresh();
		String expires=SPUtils.getRdLoginExpires();
		//验证码登录、QQ登录、微信登录、微博登录，如果不是空的，说明已经经过了新版本的登录入口,不是覆盖安装的情况
		if(!TextUtils.isEmpty(loginType)){
			if(loginType.equals("phone")&&!TextUtils.isEmpty(SPUtils.getVerifyCode())){
				LoginUtils.getInstance().doLogin(context,SPUtils.getNum(),SPUtils.getVerifyCode(),
						System.currentTimeMillis() / 1000 + "", handler);
			}else if(loginType.equals("qq")||loginType.equals("weixin")||loginType.equals("weibo")){
				RdLoginManager loginManager=new RdLoginManager(context);
				if(!TextUtils.isEmpty(openid)){
					if(loginManager!=null)
						loginManager.doRdLogin(System.currentTimeMillis()/1000+"", loginType, openid, access, expires,refresh,handler,loginType);
				}else{
					FS.getInstance().logout();
				}
			}else{
				FS.getInstance().logout();
			}
		}else{//如果是空的，说明是覆盖安装（两种情况：没有加入验证码登录之前-》本版本、加入验证码登录之后-》本版本（加入了第三方登录））
			if(TextUtils.isEmpty(SPUtils.getVerifyCode())){//没有加入验证码登录之前,使用手机号和密码登录
				MyTaskUtils.getInstance().loginInfo2(SPUtils.getUID(), SPUtils.getPassWord(),handler);
			}else{
				LoginUtils.getInstance().doLogin(context,SPUtils.getNum(),SPUtils.getVerifyCode(),
						System.currentTimeMillis() / 1000 + "", handler);
			}
		}
	}
}
